﻿using System;

using System.Windows.Forms;
using System.Text.RegularExpressions;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Collections.Generic;
using WordDoc = Microsoft.Office.Interop.Word.Document;

namespace WordTemplate2
{
    public partial class ThisDocument
    {
        public static String index_filename = "";
        public static object varMissing = System.Type.Missing;
        public static object varFalseValue = false;
        public static object varTrueValue = true;

        public static List<string> exist_refrence_list = new List<string>();


        public bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }

        public string GetWordFilePath(string template_name)
        {
            //load template
            string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            _CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 21);
            _CodeBase += "Template/";
            _CodeBase += template_name;

            return _CodeBase;
        }

        ///<summary>
        /// 在word 中查找一个字符串直接替换所需要的文本
        /// </summary>
        /// <param name="strOldText">原文本</param>
        /// <param name="strNewText">新文本</param>
        /// <returns></returns>
        public bool Replace(ref WordDoc doc, string strOldText, string strNewText)
        {
            object replaceAll = Word.WdReplace.wdReplaceAll;
            #region 文字区域
            
            Globals.ThisDocument.Application.Selection.Find.ClearFormatting();
            Globals.ThisDocument.Application.Selection.Find.Text = strOldText;

            Globals.ThisDocument.Application.Selection.Find.Replacement.ClearFormatting();
            Globals.ThisDocument.Application.Selection.Find.Replacement.Text = strNewText;

            Globals.ThisDocument.Application.Selection.Find.Execute(
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref replaceAll, ref missing, ref missing, ref missing, ref missing);
            #endregion



            #region 文本框
            Word.StoryRanges sr = doc.StoryRanges;

            foreach (Word.Range r in sr)
            {
                Word.Range r1 = r;
                if (Word.WdStoryType.wdTextFrameStory == r.StoryType)
                {
                    do
                    {
                        r1.Find.ClearFormatting();
                        r1.Find.Text = strOldText;

                        r1.Find.Replacement.ClearFormatting();
                        r1.Find.Replacement.Text = strNewText;

                        r1.Find.Execute(
                            ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref replaceAll, ref missing, ref missing, ref missing, ref missing);

                        r1 = r1.NextStoryRange;
                    } while (r1 != null);
                }
            }
            #endregion
            return true;

        }

        public void ChangeHeader(String header_name)
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object count = 1;

            Word.Range r = Globals.ThisDocument.GetBookmarkRange("mbookmark_header");
            if (r != null)
            {
                r.Text = header_name;
                object range = r;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark_header", ref range);
            }
            else
            {
                MessageBox.Show("找不到bookmark_header");
            }
        }

        public void ChangeLandScapeHeader(String header_name)
        {
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object count = 1;

            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name.Contains("mbookmark8_header_"))
                {
                    String temp_name = b.Name;
                    b.Select();

                    if (Globals.ThisDocument.Application.Selection.Text != header_name)
                    {
                        Globals.ThisDocument.Application.Selection.TypeText(header_name);
                        Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref count, ref extend);

                        object r_temp = Globals.ThisDocument.Application.Selection.Range;
                        Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add(temp_name, ref r_temp);
                    }

                }
            }
        }

        public void ContinueFooter()
        {
            Globals.ThisDocument.Application.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
            {
                Globals.ThisDocument.Application.Selection.HeaderFooter.PageNumbers.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleArabic;
                Globals.ThisDocument.Application.Selection.HeaderFooter.PageNumbers.HeadingLevelForChapter = 0;
                Globals.ThisDocument.Application.Selection.HeaderFooter.PageNumbers.IncludeChapterNumber = false;
                Globals.ThisDocument.Application.Selection.HeaderFooter.PageNumbers.ChapterPageSeparator = Word.WdSeparatorType.wdSeparatorHyphen;
                Globals.ThisDocument.Application.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = false;
                Globals.ThisDocument.Application.Selection.HeaderFooter.PageNumbers.StartingNumber = 0;

            }
            Globals.ThisDocument.Application.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
        }

        public void ResetHeaderLandscape()
        {
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object number = 1;

            Globals.ThisDocument.Application.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
            {
                Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref extend);
                Globals.ThisDocument.Application.Selection.TypeText(ThisDocument.current_header);
              
                Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref number, ref extend);
                int ref_count = GetBookmarkLandscapeCount() + 1;
                String str_bookmark_name = "mbookmark8_header_" + ref_count;
                object r_temp = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add(str_bookmark_name, ref r_temp);
            }
            Globals.ThisDocument.Application.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
        }

        public WordDoc GetMasterDocument()
        {
            System.Collections.IEnumerator dou = Globals.ThisDocument.Application.Documents.GetEnumerator();
            while (dou.MoveNext())
            {
                Word.Document d = (Word.Document)dou.Current;
                if (d.FullName == ThisDocument.index_filename)
                {
                    return d;
                }
            }
            return null;
        }

        public void CleanFields(Word.Fields fields)
        {
            System.Collections.IEnumerator c = fields.GetEnumerator();
            while (c.MoveNext())
            {
                Word.Field f = (Word.Field)c.Current;
                f.Delete();
            }
        }

        public Office.CommandBar GetCommandBar(Word.Application application, string c_bar_name)
        {
            System.Collections.IEnumerator c = application.CommandBars.GetEnumerator();
            while (c.MoveNext())
            {
                Office.CommandBar c_bar = (Office.CommandBar)c.Current;
                if (c_bar.Name == c_bar_name)
                {
                    return c_bar;
                }
            }
            return null;
        }

        public Word.Shape GetShape(string shape_name)
        {
            System.Collections.IEnumerator c = Globals.ThisDocument.Application.ActiveDocument.Shapes.GetEnumerator();
            while (c.MoveNext())
            {
                Word.Shape c_shape = (Word.Shape)c.Current;
                if (c_shape.Name == shape_name)
                {
                    return c_shape;
                }
            }
            return null;
        }

        public void RemoveHeaderFooterShapes()
        {
            System.Collections.IEnumerator c = Globals.ThisDocument.Application.ActiveDocument.Shapes.GetEnumerator();
            while (c.MoveNext())
            {
                Word.Shape c_shape = (Word.Shape)c.Current;
                if (c_shape.Name.Contains("header_") || c_shape.Name.Contains("footer_"))
                {
                    c_shape.Delete();
                }
            }
        }

        public bool CheckBookmarkExist(string mark_name)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name)
                {
                    return true;
                }
            }
            return false;
        }

        public bool RemoveBookmark(string mark_name)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name)
                {
                    b.Delete();
                    return true;
                }
            }
            return false;
        }

        public bool ReplaceBookmarkContentWithString(string mark_name, string data)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name)
                {
                    b.Range.Text = data;
                    return true;
                }
            }
            return false;
        }

        public bool ReplaceBookmarkContentWithStringKeepBookmark(string mark_name, string data)
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object character = Word.WdUnits.wdCharacter;


            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name)
                {
                    b.Range.Select();
                    b.Range.Text = data;
                    object number = data.Length;
                    Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref number, ref extend);

                    object r = Globals.ThisDocument.Application.Selection.Range;
                    Globals.ThisDocument.Application.Selection.Bookmarks.Add(mark_name, ref r);
                    return true;
                }
            }
            return false;
        }

        public bool CheckIsInBookmarkRange(string mark_name)
        {
            Word.Range r = Globals.ThisDocument.Application.Selection.Range;
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name && (b.Range.Start < r.Start && b.Range.End > r.End))
                {
                    return true;
                }
            }
            return false;
        }

        public Word.Range GetBookmarkRange(string mark_name)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name)
                {
                    return b.Range;
                }
            }
            return null;
        }

        public int GetBookmarkLandscapeCount()
        {
            int bookmark_count = 0;
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                 Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                 if (b.Name.Contains("mbookmark8_header_"))
                 {
                     bookmark_count++;
                 }
            }

            return bookmark_count;
        }

        public bool SelectionParseFromFile(string filename)
        {
            object varFileName = (object)GetWordFilePath(filename);
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            try
            {
                Word.Document varDoc =
                       Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);

                Word.Range range = varDoc.Range(ref varMissing, ref varMissing);

                range.Copy();

                ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);

                doc.Application.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
                
            }
            catch
            {
            	return false;
            }

            return true;
        }

        public bool SelectionParseFromFileBookmark(string filename, string bookmark_name, bool remove_bookmark)
        {
            object varFileName = (object)GetWordFilePath(filename);
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            try
            {
                Word.Document varDoc =
                       Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);

                System.Collections.IEnumerator bookmark = varDoc.Bookmarks.GetEnumerator();
                while (bookmark.MoveNext())
                {
                    Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                    if (b.Name == bookmark_name)
                    {
                        Word.Bookmark b1 = b.Copy("temp");
                        Word.Range r = b1.Range;
                        
                        r.Copy();
                    }
                }

                ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);

                doc.Application.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool SelectionParseFromTextureCenter(string filename)
        {
            string varFileName = GetWordFilePath(filename);
            try
            {
                Globals.ThisDocument.Application.Selection.InlineShapes.AddPicture(varFileName, ref varMissing, ref varTrueValue, ref varMissing);

                Globals.ThisDocument.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool ReplaceBookmarkContentWithFile(string mark_name, string filename)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            object varFileName = (object)GetWordFilePath(filename);

            Word.Document varDoc =
                    Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);

            Word.Range range = varDoc.Range(ref varMissing, ref varMissing);

            range.Copy();

            System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name)
                {
                    b.Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

                    ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);

                    return true;
                }
            }

            ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);
            return false;
        }

        public void SaveTempFile(string target_file_name)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();
            object target_path = target_file_name;
            object varFileName = (object)GetWordFilePath("temp.doc");

            Word.Document varDoc =
                    Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);

            Word.Range temp_range = varDoc.Range(ref varMissing, ref varMissing);
            Word.Range range1 = doc.Range(ref varMissing, ref varMissing);
            range1.Copy();
            temp_range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

            varDoc.SaveAs(ref target_path, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing
                    , ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);

            ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);
        }

        public bool ReplaceBookmarkContentWithFileBookmark(string mark_name, string filename)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            object varFileName = (object)GetWordFilePath(filename);

            Word.Document varDoc =
                    Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);

            {
                System.Collections.IEnumerator bookmark = varDoc.Bookmarks.GetEnumerator();
                while (bookmark.MoveNext())
                {
                    Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                    if (b.Name == mark_name)
                    {
                        b.Range.Copy();
                    }
                }
            }

            {
                System.Collections.IEnumerator bookmark = doc.Bookmarks.GetEnumerator();
                while (bookmark.MoveNext())
                {
                    Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                    if (b.Name == mark_name)
                    {
                        b.Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

                        ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);

                        return true;
                    }
                }
            }


            ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);
            return false;
        }

        public bool CopyDataFromFileFullPath(string path)
        {
            try
            {
                WordDoc doc = Globals.ThisDocument.GetMasterDocument();

                object varFileName = path;

                Word.Document varDoc =
                    Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);

                Word.Range range = varDoc.Range(ref varMissing, ref varMissing);

                range.Copy();

                Word.Range range1 = doc.Range(ref varMissing, ref varMissing);

                range1.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

                ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);

            }
            catch (System.Exception e)
            {
                string error = String.Format("Copy Data Full Path Failed s'{0}'.", e.Message);
                MessageBox.Show(error);
                return false;
            }
            return true;
        }

        public bool CopyDataFromTemplate(string template_name)
        {
            try
            {
                WordDoc doc = Globals.ThisDocument.GetMasterDocument();

                object varFileName = (object)GetWordFilePath(template_name);

                Word.Document varDoc =
                    Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);

                Word.Range range = varDoc.Range(ref varMissing, ref varMissing);
                range.Copy();

                Word.Range range1 = doc.Range(ref varMissing, ref varMissing);

                range1.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
                ((Word._Document)varDoc).Close(ref varFalseValue, ref varMissing, ref varMissing);
               
            }
            catch (Exception ex)
            {
                string error = String.Format("Copy Data Failed s'{0}'.", ex.Message);
                MessageBox.Show(error);
                return false;
            }
            return true;
        }

        public void OpenNewTemplate(string template_name)
        {
            WordDoc doc = Globals.ThisDocument.GetMasterDocument();

            object varFileName = (object)GetWordFilePath(template_name);

            Globals.ThisDocument.Application.Documents.Add(ref varFileName, ref varMissing, ref varMissing, ref varMissing);
        }

        public bool CheckPageIsLandscape()
        {
            Word.PageSetup setup = Globals.ThisDocument.Application.Selection.PageSetup;
           
            if(setup.Orientation == Word.WdOrientation.wdOrientLandscape)
            {
                return true;
            }
            return false;
        }

        public bool MoveBookmarkStart(string mark)
        {
            System.Collections.IEnumerator bookmark = Globals.ThisDocument.GetMasterDocument().Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark)
                {
                    Globals.ThisDocument.Application.Selection.Start = b.Start;
                    Globals.ThisDocument.Application.Selection.End = b.Start;
                    return true;
                }
            }
            return false;
        }

        public bool MoveBookmarkSelected(string mark)
        {
            System.Collections.IEnumerator bookmark = Globals.ThisDocument.GetMasterDocument().Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Word.Bookmark b = (Word.Bookmark)bookmark.Current;
                if (b.Name == mark)
                {
                    b.Select();
                    return true;
                }
            }
            return false;
        }

        public bool MoveBookmarkEnd(string mark_name)
        {
            System.Collections.IEnumerator bookmark = Globals.ThisDocument.GetMasterDocument().Bookmarks.GetEnumerator();
            while (bookmark.MoveNext())
            {
                Microsoft.Office.Interop.Word.Bookmark b = (Microsoft.Office.Interop.Word.Bookmark)bookmark.Current;
                if (b.Name == mark_name)
                {
                    Globals.ThisDocument.Application.Selection.Start = b.End;
                    Globals.ThisDocument.Application.Selection.End = b.End;
                    return true;
                }
            }
            return false;
        }


        public void DeleteSelectionWindow()
        {
            object window = (object)Word.WdUnits.wdWindow;
            object extend = (object)Word.WdMovementType.wdExtend;
            object move = (object)Word.WdMovementType.wdMove;
            Globals.ThisDocument.Application.Selection.MoveUp(ref window, ref varMissing, ref move);
            Globals.ThisDocument.Application.Selection.MoveDown(ref window, ref varMissing, ref extend);
            Globals.ThisDocument.Application.Selection.Delete(ref varMissing, ref varMissing);
        }

        public void SetParagraphSpecial()
        {
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LeftIndent = Globals.ThisDocument.Application.CentimetersToPoints(0.9f);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.RightIndent = Globals.ThisDocument.Application.CentimetersToPoints(-0.09f);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceBefore = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceBeforeAuto = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceAfter = 1.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceAfterAuto = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LineSpacing = 15;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
          
            Globals.ThisDocument.Application.Selection.ParagraphFormat.WidowControl = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.KeepWithNext = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.KeepTogether = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.PageBreakBefore = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.NoLineNumber = 0;
            //Globals.ThisDocument.Application.Selection.ParagraphFormat.Hyphenation = 1;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.FirstLineIndent = Globals.ThisDocument.Application.CentimetersToPoints(-0.64f);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;

            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = 1.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitRightIndent = -0.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LineUnitBefore = 0f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LineUnitAfter = 0.3f;
            //Globals.ThisDocument.Application.Selection.ParagraphFormat.AutoAdjustRightIndent = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.DisableLineHeightGrid = 0;

            Globals.ThisDocument.Application.Selection.ParagraphFormat.FarEastLineBreakControl = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.WordWrap = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.HangingPunctuation = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.HalfWidthPunctuationOnTopOfLine = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndDigit = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.BaseLineAlignment = Word.WdBaselineAlignment.wdBaselineAlignAuto;
        }

        public void SetParagraphAppendix()
        {
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LeftIndent = Globals.ThisDocument.Application.CentimetersToPoints(-0.09f);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.RightIndent = Globals.ThisDocument.Application.CentimetersToPoints(-0.09f);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceBefore = 2.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceBeforeAuto = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceAfter = 2.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.SpaceAfterAuto = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            //Globals.ThisDocument.Application.Selection.ParagraphFormat.LineSpacing = 15;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;

            //Globals.ThisDocument.Application.Selection.ParagraphFormat.WidowControl = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.KeepWithNext = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.KeepTogether = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.PageBreakBefore = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.NoLineNumber = 0;
            //Globals.ThisDocument.Application.Selection.ParagraphFormat.Hyphenation = 1;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.FirstLineIndent = Globals.ThisDocument.Application.CentimetersToPoints(0);
            Globals.ThisDocument.Application.Selection.ParagraphFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel2;

            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitLeftIndent = -0.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitRightIndent = -0.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LineUnitBefore = 0.5f;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.LineUnitAfter = 0.5f;
            //Globals.ThisDocument.Application.Selection.ParagraphFormat.AutoAdjustRightIndent = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.DisableLineHeightGrid = 0;

            //Globals.ThisDocument.Application.Selection.ParagraphFormat.FarEastLineBreakControl = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.WordWrap = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.HangingPunctuation = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.HalfWidthPunctuationOnTopOfLine = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha = 0;
            //Globals.ThisDocument.Application.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndDigit = 0;
            Globals.ThisDocument.Application.Selection.ParagraphFormat.BaseLineAlignment = Word.WdBaselineAlignment.wdBaselineAlignBaseline;
        }

      

        public void SetAppendixListNumberFormat(int level)
        {
            Word.ListTemplate t = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListTemplate;
            bool dirty = false;
            if (t == null)
            {
                Globals.ThisDocument.Application.Selection.Range.ListFormat.ApplyOutlineNumberDefault(ref varMissing);
                t = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListTemplate;
            }

            {
                Word.ListLevel l = t.ListLevels[1];

                string value = "附 录 %1";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleUppercaseLetter;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 0;
                l.StartAt = 1;

                l.LinkedStyle = "标准文件_附录标识";
            }

            {
                Word.ListLevel l = t.ListLevels[2];
                string value = "%1.%2　";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 1;
                l.StartAt = 1;

                l.LinkedStyle = "标准文件_附录章标题";
            }

            {
                Word.ListLevel l = t.ListLevels[3];
                string value = "%1.%2.%3　";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 2;
                l.StartAt = 1;

                l.LinkedStyle = "标准文件_附录一级条标题";
            }

            {
                Word.ListLevel l = t.ListLevels[4];
                string value = "%1.%2.%3.%4　";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 3;
                l.StartAt = 1;

                l.LinkedStyle = "标准文件_附录二级条标题";
            }

            {
                Word.ListLevel l = t.ListLevels[5];
                string value = "%1.%2.%3.%4.%5　";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 4;
                l.StartAt = 1;

                l.LinkedStyle = "标准文件_附录三级条标题";
            }

            {
                Word.ListLevel l = t.ListLevels[6];
                string value = "%1.%2.%3.%4.%5.%6　";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 5;
                l.StartAt = 1;

                l.LinkedStyle = "标准文件_附录四级条标题";
            }

            {
                Word.ListLevel l = t.ListLevels[7];
                string value = "%1.%2.%3.%4.%5.%6.%7　";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 6;
                l.StartAt = 1;

                l.LinkedStyle = "标准文件_附录五级条标题";
            }

            {
                Word.ListLevel l = t.ListLevels[8];
                string value = "%1.%2.%3.%4.%5.%6.%7.%8";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 7;
                l.StartAt = 1;

            }

            {
                Word.ListLevel l = t.ListLevels[9];

                string value = "%1.%2.%3.%4.%5.%6.%7.%8.%9";
                if (l.NumberFormat != value)
                {
                    dirty = true;
                    l.NumberFormat = value;
                }
                l.TrailingCharacter = Word.WdTrailingCharacter.wdTrailingNone;
                l.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;

                l.Alignment = Word.WdListLevelAlignment.wdListLevelAlignLeft;
                l.NumberPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TabPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.TextPosition = Globals.ThisDocument.Application.CentimetersToPoints(0);
                l.ResetOnHigher = 8;
                l.StartAt = 1;

            }

            if (dirty)
            {
                object applyto = Word.WdListApplyTo.wdListApplyToWholeList;
                Globals.ThisDocument.Application.Selection.Range.ListFormat.ApplyListTemplate(t, ref varTrueValue, ref applyto, ref varMissing);
            }
            
            Globals.ThisDocument.Application.Selection.Range.ListFormat.ListLevelNumber = level;
            SetParagraphAppendix();
        }

        public void LoadExistRefrence()
        {
            object line = Word.WdUnits.wdLine;
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object count = 1;
            exist_refrence_list.Clear();

            Globals.ThisDocument.MoveBookmarkStart("mbookmark4_ref");
            Word.Range r = Globals.ThisDocument.GetBookmarkRange("mbookmark4_ref");
            Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref count, ref move);

            while (Globals.ThisDocument.Application.Selection.Range.End < r.End)
            {
                Globals.ThisDocument.Application.Selection.EndKey(ref line, ref move);
                Globals.ThisDocument.Application.Selection.HomeKey(ref line, ref extend);
                if(Globals.ThisDocument.Application.Selection.Text.Contains(" "))
                    exist_refrence_list.Add(Globals.ThisDocument.Application.Selection.Text);
                
                Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref count, ref move);               
            }
        }

        public void ClearExistRefrence()
        {
            exist_refrence_list.Clear();
        }

        public string GetAppendixText()
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object count = 1;

            object r_temp = Globals.ThisDocument.Application.Selection.Range;
            Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("temp_fulu", ref r_temp);
            string mark = "";
            for (uint i = 0; i < 10000; i++)
            {
                string str1 = Globals.ThisDocument.Application.Selection.Range.ListFormat.ListString;
                if (str1.Contains("附"))
                {
                    mark = str1.Substring(str1.LastIndexOf("录") + 2);
                    break;
                }
                Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref count, ref move);
            }

            MoveBookmarkSelected("temp_fulu");
            Globals.ThisDocument.RemoveBookmark("temp_fulu");

            return mark;
        }
    }
}
